package com.cqc.jdk8.Jtime;

import com.cqc.util.GsonUtil;
import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * author: cui
 * Date: 2022/1/21
 * Time: 14:46
 **/
public class LocalDateTimeTest {

    @Test
    public void date2LocalDateTime() {
        Date date = new Date();
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println("date转LocalDateTime后格式化：" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        LocalDateTime dateTime = LocalDateTime.now();
        ZonedDateTime zone = dateTime.atZone(ZoneId.systemDefault());
        Date d = Date.from(zone.toInstant());
        System.out.println("LocalDateTime转Date:" + d);
    }

    //格式化
    @Test
    public void format() {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("toString=" + dateTime.toString());//2022-05-24T22:08:38.067
        System.out.println("ISO_TIME=" + dateTime.format(DateTimeFormatter.ISO_TIME));//21:57:12.058
        System.out.println("ISO_LOCAL_TIME=" + dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME));//21:57:12.058

        System.out.println("BASIC_ISO_DATE=" + dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));//20240629
        System.out.println("ISO_DATE=" + dateTime.format(DateTimeFormatter.ISO_DATE));//2024-06-29
        System.out.println("ISO_LOCAL_DATE=" + dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE));//2024-06-29
        System.out.println("ISO_LOCAL_DATE_TIME=" + dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));//2022-05-24T21:57:12.058

        //自定义格式的  格式化日期时间
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2022-05-24 22:02:28
    }


    //解析
    @Test
    public void parse() {
        //注意：toString()含有T,需要手动去除T  replace("T"," ")
        LocalDateTime dateTime = LocalDateTime.parse("2022-01-02 10:30:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(dateTime.toString());//2022-01-02T10:30
    }


    @Test
    public void toJson() {
        LocalDateTime now = LocalDateTime.now();
        //{"date":{"year":2023,"month":4,"day":4},"time":{"hour":10,"minute":32,"second":18,"nano":453000000}}
        System.out.println(GsonUtil.toJson(now));
        //"Apr 4, 2023 10:32:18 AM"
        System.out.println(GsonUtil.toJson(new Date()));
    }

    @Test
    public void atTime() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime min = now.toLocalDate().atTime(LocalTime.MIN);
        LocalDateTime noon = now.toLocalDate().atTime(LocalTime.NOON);
        LocalDateTime midNight = now.toLocalDate().atTime(LocalTime.MIDNIGHT);
        LocalDateTime max = now.toLocalDate().atTime(LocalTime.MAX);

        System.out.println("min=" + min.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 00:00:00.000
        System.out.println("noon=" + noon.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 12:00:00.000
        System.out.println("midNight=" + midNight.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 00:00:00.000
        System.out.println("max=" + max.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));//2024-06-29 23:59:59.999

        //没有  .atEndOfDay();
        LocalDateTime dateTime3 = now.toLocalDate().atStartOfDay();
        //2024-04-21 00:00:00
        System.out.println(dateTime3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

}
